package cn.zhentao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.zhentao.pojo.TCarBrand;
import cn.zhentao.service.TCarBrandService;

import java.util.List;

@RestController
@RequestMapping("carBrand")
@CrossOrigin(origins = "*") // 允许跨域访问
public class CarBrandController {

    @Autowired
    private TCarBrandService tCarBrandService;
    
    
        /**查询所有 附带分页 条件*/
        @PostMapping("findList")
        public Page<TCarBrand> findList(@RequestBody TCarBrand carBrand){
            Page<TCarBrand> page = new Page<>(carBrand.getCurrent(),carBrand.getSize());
            QueryWrapper<TCarBrand> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(carBrand.getBrandName()!=null,"brand_name",carBrand.getBrandName());
            Page<TCarBrand> page1 = tCarBrandService.page(page, queryWrapper);
            List<TCarBrand> records = page1.getRecords();
            return page1.setRecords(records);
        }
        /**
         * 添加
         * @param carBrand 信息
         * @return 添加结果
         */
    
        @PostMapping("add")
        public String add(@RequestBody TCarBrand carBrand) {
            boolean save = tCarBrandService.save(carBrand);
            return save ? "添加成功" : "添加失败";
        }
    
    
    
        /**
         * 修改
         * @param carBrand 信息
         * @return 修改结果
         */
        @PostMapping("update")
        public String update(@RequestBody TCarBrand carBrand) {
            boolean update = tCarBrandService.updateById(carBrand);
            return update ? "修改成功" : "修改失败";
        }
    
    
        /**
         * 根据ID查询
         * @param id ID
         * @return 信息
         */
        @GetMapping("findById")
        public TCarBrand findById(@RequestParam Integer id) {
            return tCarBrandService.getById(id);
        }
    
        /**
         * 删除
         * @param id ID
         * @return 删除结果
         */
        @GetMapping("delete")
        public String delete(@RequestParam Integer id) {
            boolean delete = tCarBrandService.removeById(id);
            return delete ? "删除成功" : "删除失败";
        }

    /**
     * 查询所有数据，不进行分页
     *
     * @return 所有符合条件的数据列表
     */
    @GetMapping("findAll")
    public List<TCarBrand> findAll() {
        QueryWrapper<TCarBrand> queryWrapper = new QueryWrapper<>();
        return tCarBrandService.list(queryWrapper);
    }
}
